home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr05 / keycal.zip / GENERIC.C < prev    next >
C/C++ Source or Header  |  1993-07-04  |  21KB  |  446 lines

  1. /****************************************************************************
  2.   PROGRAM: Generic.c
  3.   PURPOSE: Generic template for Windows applications
  4.   FUNCTIONS:
  5.   WinMain() - calls initialization function, processes message loop
  6.   InitApplication() - initializes window data and registers window
  7.   InitInstance() - saves instance handle and creates main window
  8.   MainWndProc() - processes messages
  9.   About() - processes messages for "About" dialog box
  10.   COMMENTS:
  11.       Windows can have several copies of your application running at the
  12.       same time.  The variable hInst keeps track of which instance this
  13.       application is so that processing will be to the correct window.
  14. ****************************************************************************/
  15. #include <windows.h>            /* required for all Windows applications */
  16. #include <ctl3d.h>                        /* 3d controls dll prototypes    */
  17. #include "generic.h"                      /* specific to this program      */
  18.  
  19. #include "KeyCal.h"           // control message constants
  20.  
  21. HANDLE hInst;                             /* current instance              */
  22.  
  23. /*********************************************/
  24. /* variables used by the calendar dialog box */
  25. /* be careful how much stuff you put on the  */
  26. /* stack with windows routines!              */
  27. /*********************************************/
  28. static long l, lDays[ 31 ];
  29. static int  nDays[ 31 ];
  30. static char sz[ 64 ];
  31.  
  32. /***********************************************************************/
  33. /* This routine was added to the generic application template provided */
  34. /* by Microsoft. The rest of the program is a very simple example of a */
  35. /* Windows program.                                                    */
  36. /*                                                                     */
  37. /* This routine intercepts notification messages from the calendar and */
  38. /* displays the julian date number in an edit box below the calendar.  */
  39. /*                                                                     */
  40. /* A test button is provided that runs a suite of tests against the    */
  41. /* control to check it's operation. The code as written only works     */
  42. /* for the day that it was written for. It was the only way to test    */
  43. /* some of the functions without getting ridiculous.                   */
  44. /*                                                                     */
  45. /* A couple of selection buttons allow you to disallow selection of    */
  46. /* certain days of the week.                                           */
  47. /*                                                                     */
  48. /* This code may be freely used as an example in your own program      */
  49. /***********************************************************************/
  50. BOOL FAR PASCAL Calendar( HWND hWnd, unsigned message, WORD wParam, LONG lParam )
  51.   {
  52.     switch ( message )
  53.       {
  54.         case WM_INITDIALOG:
  55.           return ( TRUE );
  56.  
  57.         case WM_COMMAND:
  58.           switch ( wParam )
  59.             {
  60.               /* user pressed the test button */
  61.               case IDD_TEST:
  62.                 /* send reset and check that one date is selected */
  63.                 SendDlgItemMessage( hWnd, IDD_CAL, DM_RESET, 0, 0L );
  64.  
  65.                 /* test all the functions */
  66.  
  67.                 if ( SendDlgItemMessage( hWnd, IDD_CAL, DM_GET_MONTH, 0, 0L ) != 4 )
  68.                   MessageBox( NULL, "DM_GET_MONTH does not return april", NULL, MB_ICONEXCLAMATION );
  69.  
  70.                 if ( SendDlgItemMessage( hWnd, IDD_CAL, DM_GET_YEAR, 0, 0L ) != 1993 )
  71.                   MessageBox( NULL, "DM_GET_YEAR does not return 1993", NULL, MB_ICONEXCLAMATION );
  72.  
  73.                 if ( SendDlgItemMessage( hWnd, IDD_CAL, DM_GET_DAY, 0, 0L ) != 8 )
  74.                   MessageBox( NULL, "DM_GET_DAY does not return 8th", NULL, MB_ICONEXCLAMATION );
  75.  
  76.                 l = SendDlgItemMessage( hWnd, IDD_CAL, DM_GETSELITEMS, 31, (LONG)(LPINT)nDays );
  77.                 if ( l != 1 )
  78.                   {
  79.                     wsprintf( sz, "DM_GETSELITEMS returns %ld ( %d )", l, nDays[ 0 ] );
  80.                     MessageBox( NULL, sz, NULL, MB_ICONEXCLAMATION );
  81.                   }
  82.                 if ( nDays[ 0 ] != 8 )
  83.                   {
  84.                     wsprintf( sz, "DM_GETSELITEMS returns wrong selected item %d", nDays[ 0 ] );
  85.                     MessageBox( NULL, sz, NULL, MB_ICONEXCLAMATION );
  86.                   }
  87.  
  88.                 l = SendDlgItemMessage( hWnd, IDD_CAL, DM_GETSELDATES, 31, (LONG)(LPLONG)lDays );
  89.                 if ( l != 1 )
  90.                   {
  91.                     wsprintf( sz, "DM_GETSELDATES returns %ld ( %ld )", l, lDays[ 0 ] );
  92.                     MessageBox( NULL, sz, NULL, MB_ICONEXCLAMATION );
  93.                   }
  94.  
  95.                 if ( SendDlgItemMessage( hWnd, IDD_CAL, DM_GET_DAYS, 0, 0L ) != 0 )
  96.                   MessageBox( NULL, "DM_GET_DAYS does not return 0", NULL, MB_ICONEXCLAMATION );
  97.  
  98.  
  99.                 /* set the date and try tests again */
  100.                 SendDlgItemMessage( hWnd, IDD_CAL, DM_SET_DATE, 0, 2444605L );
  101.  
  102.                 if ( SendDlgItemMessage( hWnd, IDD_CAL, DM_GET_MONTH, 0, 0L ) != 1 )
  103.                   MessageBox( NULL, "DM_GET_MONTH does not return January", NULL, MB_ICONEXCLAMATION );
  104.  
  105.                 if ( SendDlgItemMessage( hWnd, IDD_CAL, DM_GET_YEAR, 0, 0L ) != 1981 )
  106.                   MessageBox( NULL, "DM_GET_YEAR does not return 1981", NULL, MB_ICONEXCLAMATION );
  107.  
  108.                 if ( SendDlgItemMessage( hWnd, IDD_CAL, DM_GET_DAY, 0, 0L ) != 1 )
  109.                   MessageBox( NULL, "DM_GET_DAY does not return 1st", NULL, MB_ICONEXCLAMATION );
  110.  
  111.                 l = SendDlgItemMessage( hWnd, IDD_CAL, DM_GETSELITEMS, 31, (LONG)(LPINT)nDays );
  112.                 if ( l != 1 )
  113.                   {
  114.                     wsprintf( sz, "DM_GETSELITEMS returns %ld ( %d )", l, nDays[ 0 ] );
  115.                     MessageBox( NULL, sz, NULL, MB_ICONEXCLAMATION );
  116.                   }
  117.                 if ( nDays[ 0 ] != 1 )
  118.                   {
  119.                     wsprintf( sz, "DM_GETSELITEMS returns wrong selected item %d", nDays[ 0 ] );
  120.                     MessageBox( NULL, sz, NULL, MB_ICONEXCLAMATION );
  121.                   }
  122.  
  123.                 l = SendDlgItemMessage( hWnd, IDD_CAL, DM_GETSELDATES, 31, (LONG)(LPLONG)lDays );
  124.                 if ( l != 1 )
  125.                   {
  126.                     wsprintf( sz, "DM_GETSELDATES returns %ld ( %ld )", l, lDays[ 0 ] );
  127.                     MessageBox( NULL, sz, NULL, MB_ICONEXCLAMATION );
  128.                   }
  129.  
  130.                 if ( SendDlgItemMessage( hWnd, IDD_CAL, DM_GET_DAYS, 0, 0L ) !=  ( 2449085L - 2444605L ) )
  131.                   MessageBox( NULL, "DM_GET_DAYS does not return correct value", NULL, MB_ICONEXCLAMATION );
  132.  
  133.                 MessageBox( NULL, "All function tests complete", NULL, MB_ICONEXCLAMATION );
  134.  
  135.                 break;
  136.  
  137.               /* any day of the week is selectable */
  138.               case IDD_ANY:
  139.                 SendDlgItemMessage( hWnd, IDD_CAL, DM_SELECT_ANY, 0, 0L );
  140.                 break;
  141.  
  142.               /* only weekends are selectable */
  143.               case IDD_WEEKEND:
  144.                 SendDlgItemMessage( hWnd, IDD_CAL, DM_SELECT_DAY_MASK, 0, (LONG)0x81 );
  145.                 break;
  146.  
  147.               /* only weekdays are selectable */
  148.               case IDD_WEEKDAY:
  149.                 SendDlgItemMessage( hWnd, IDD_CAL, DM_SELECT_DAY_MASK, 0, (LONG)0x3E );
  150.                 break;
  151.  
  152.               /* calendar notification messages */
  153.               case IDD_CAL:
  154.                 /* high order word of lParam contains notification code */
  155.                 /* low order word of lParam contains window handle */
  156.                 switch ( HIWORD( lParam ) )
  157.                   {
  158.                     case DN_DATECHANGED:
  159.                       // get new date from date control
  160.                       l = SendMessage( LOWORD( lParam ), DM_GET_DATE, 0, 0L );
  161.  
  162.                       // put date into text control
  163.                       wsprintf( sz, "%ld", l );
  164.                       SetDlgItemText( hWnd, IDD_DATETEXT, (LPSTR) sz );
  165.                       break;
  166.                   }
  167.                 break;
  168.  
  169.               case IDD_HELP:
  170.                 WinHelp( hWnd, "keycal.hlp", HELP_INDEX, 0L );
  171.                 break;
  172.  
  173.               case IDOK:
  174.                 EndDialog( hWnd, TRUE );
  175.                 return ( TRUE );
  176.                 break;
  177.  
  178.               case IDCANCEL:
  179.                 EndDialog( hWnd, TRUE );
  180.                 return ( TRUE );
  181.                 break;
  182.             }
  183.           break;
  184.       }
  185.     return ( FALSE );
  186.   }
  187.  
  188. /****************************************************************************
  189.   FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  190.   PURPOSE: calls initialization function, processes message loop
  191.   COMMENTS:
  192.     Windows recognizes this function by name as the initial entry point 
  193.     for the program.  This function calls the application initialization 
  194.     routine, if no other instance of the program is running, and always 
  195.     calls the instance initialization routine.  It then executes a message 
  196.     retrieval and dispatch loop that is the top-level control structure 
  197.     for the remainder of execution.  The loop is terminated when a WM_QUIT 
  198.     message is received, at which time this function exits the application 
  199.     instance by returning the value passed by PostQuitMessage(). 
  200.     If this function must abort before entering the message loop, it 
  201.     returns the conventional value NULL.  
  202. ****************************************************************************/
  203. int PASCAL WinMain( HANDLE hInstance,       /* current instance         */
  204.                     HANDLE hPrevInstance,   /* previous instance         */
  205.                     LPSTR lpCmdLine,        /* command line             */
  206.                     int nCmdShow            /* show-window type (open/icon) */
  207.                   )
  208.   {
  209.   HANDLE   hLibrary;
  210.   MSG msg;                     /* message                 */
  211.  
  212.     /****************************************************/
  213.     /* The following two calls use the 3d look controls */
  214.     /* provided by Microsoft in ctl3d.dll               */
  215.     /****************************************************/
  216.     Ctl3dRegister( hInstance );         /* use new 3d look controls        */
  217.     Ctl3dAutoSubclass( hInstance );
  218.  
  219.     if ( ! hPrevInstance )             /* Other instances of app running? */
  220.       if ( ! InitApplication( hInstance ) ) /* Initialize shared things */
  221.         return ( FALSE );         /* Exits if unable to initialize     */
  222.  
  223.     /* Perform initializations that apply to a specific instance */
  224.     if ( ! InitInstance( hInstance, nCmdShow ) )
  225.       return (FALSE);
  226.  
  227.     /****************************************************************/
  228.     /* This loads the calendar DLL so it can be used in the program */
  229.     /****************************************************************/
  230.     hLibrary = LoadLibrary( "KeyCal.DLL" );
  231.     if ( hLibrary < 32 )
  232.       {
  233.         MessageBox( NULL, "Cannot Load KeyCal DLL", NULL, MB_ICONEXCLAMATION );
  234.         return ( -1 );
  235.       }
  236.  
  237.     /* Acquire and dispatch messages until a WM_QUIT message is received. */
  238.     while (GetMessage(&msg,       /* message structure                 */
  239.         NULL,           /* handle of window receiving the message */
  240.         NULL,           /* lowest message to examine             */
  241.         NULL))           /* highest message to examine         */
  242.       {
  243.         TranslateMessage(&msg);           /* Translates virtual key codes  */
  244.         DispatchMessage(&msg);            /* Dispatches message to window  */
  245.       }
  246.  
  247.     /**************************************************************/
  248.     /* Don't forget to unload the calendar DLL when done using it */
  249.     /* Otherwise Windows will keep it in memory                   */
  250.     /**************************************************************/
  251.     FreeLibrary( hLibrary );
  252.  
  253.     /***********************************/
  254.     /* done using the 3d look controls */
  255.     /* unregister them to free memory  */
  256.     /***********************************/
  257.     Ctl3dUnregister( hInst );             /* remove subclassing on controls*/
  258.  
  259.     return (msg.wParam);       /* Returns the value from PostQuitMessage */
  260.   }
  261.  
  262.  
  263. /****************************************************************************
  264.   FUNCTION: InitApplication(HANDLE)
  265.   PURPOSE: Initializes window data and registers window class
  266.   COMMENTS:
  267.     This function is called at initialization time only if no other 
  268.     instances of the application are running.  This function performs 
  269.     initialization tasks that can be done once for any number of running 
  270.     instances.  
  271.     In this case, we initialize a window class by filling out a data 
  272.     structure of type WNDCLASS and calling the Windows RegisterClass() 
  273.     function.  Since all instances of this application use the same window 
  274.     class, we only need to do this when the first instance is initialized.  
  275. ****************************************************************************/
  276. BOOL InitApplication( HANDLE hInstance )
  277.   {
  278.   WNDCLASS  wc;
  279.  
  280.     wc.style = NULL;                   /* Class style(s).                   */
  281.     wc.lpfnWndProc = MainWndProc;      /* Function to retrieve messages for */
  282.                                        /* windows of this class.            */
  283.     wc.cbClsExtra = 0;                 /* No per-class extra data.          */
  284.     wc.cbWndExtra = 0;                 /* No per-window extra data.         */
  285.     wc.hInstance = hInstance;          /* Application that owns the class.  */
  286.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  287.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  288.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
  289.     wc.lpszMenuName =  "GenericMenu";   /* Name of menu resource in .RC file. */
  290.     wc.lpszClassName = "GenericWClass"; /* Name used in call to CreateWindow. */
  291.  
  292.     /* Register the window class and return success/failure code. */
  293.     return ( RegisterClass( & wc ) );
  294.   }
  295.  
  296.  
  297. /****************************************************************************
  298.   FUNCTION:  InitInstance(HANDLE, int)
  299.   PURPOSE:  Saves instance handle and creates main window
  300.   COMMENTS:
  301.     This function is called at initialization time for every instance of 
  302.     this application.  This function performs initialization tasks that 
  303.     cannot be shared by multiple instances.  
  304.     In this case, we save the instance handle in a static variable and 
  305.     create and display the main program window.  
  306. ****************************************************************************/
  307. BOOL InitInstance(hInstance, nCmdShow)
  308.     HANDLE          hInstance;          /* Current instance identifier.       */
  309.     int             nCmdShow;           /* Param for first ShowWindow() call. */
  310.   {
  311.   HWND            hWnd;               /* Main window handle.                */
  312.  
  313.     /* Save the instance handle in static variable, which will be used in  */
  314.     /* many subsequence calls from this application to Windows.            */
  315.  
  316.     hInst = hInstance;
  317.  
  318.     /* Create a main window for this application instance.  */
  319.  
  320.     hWnd = CreateWindow(
  321.         "GenericWClass",                /* See RegisterClass() call.          */
  322.         "Generic Sample Application",   /* Text for window title bar.         */
  323.         WS_OVERLAPPEDWINDOW,            /* Window style.                      */
  324.         CW_USEDEFAULT,                  /* Default horizontal position.       */
  325.         CW_USEDEFAULT,                  /* Default vertical position.         */
  326.         CW_USEDEFAULT,                  /* Default width.                     */
  327.         CW_USEDEFAULT,                  /* Default height.                    */
  328.         NULL,                           /* Overlapped windows have no parent. */
  329.         NULL,                           /* Use the window class menu.         */
  330.         hInstance,                      /* This instance owns this window.    */
  331.         NULL                            /* Pointer not needed.                */
  332.     );
  333.  
  334.     /* If window could not be created, return "failure" */
  335.  
  336.     if (!hWnd)
  337.         return (FALSE);
  338.  
  339.     /* Make the window visible; update its client area; and return "success" */
  340.  
  341.     /* Show the window */
  342.     ShowWindow(hWnd, nCmdShow );
  343.     UpdateWindow(hWnd);          /* Sends WM_PAINT message                 */
  344.     return (TRUE);               /* Returns the value from PostQuitMessage */
  345.   }
  346.  
  347. /****************************************************************************
  348.   FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
  349.   PURPOSE:  Processes messages
  350.   MESSAGES:
  351.     WM_COMMAND    - application menu (About dialog box)
  352.     WM_DESTROY    - destroy window
  353.   COMMENTS:
  354.     To process the IDM_ABOUT message, call MakeProcInstance() to get the
  355.     current instance address of the About() function.  Then call Dialog
  356.     box which will create the box according to the information in your
  357.     generic.rc file and turn control over to the About() function.    When
  358.     it returns, free the intance address.
  359. ****************************************************************************/
  360. long FAR PASCAL MainWndProc( HWND hWnd,         /* window handle           */
  361.                              unsigned message,  /* type of message         */
  362.                              WORD wParam,       /* additional information  */
  363.                              LONG lParam        /* additional information  */
  364.                            )
  365.   {
  366.   FARPROC lpProcAbout;          /* pointer to the "About" function */
  367.  
  368.     switch (message)
  369.       {
  370.         case WM_COMMAND:       /* message: command from application menu */
  371.           switch ( wParam )
  372.             {
  373.               /**********************************/
  374.               /* exit for a windows application */
  375.               /**********************************/
  376.               case IDM_F_EXIT:
  377.                 PostQuitMessage( 0 );
  378.                 break;
  379.  
  380.               case IDM_CAL:
  381.                 lpProcAbout = MakeProcInstance( Calendar, hInst );
  382.                 DialogBox( hInst,              /* current instance         */
  383.                            "CalendarTest",  /* resource to use          */
  384.                            hWnd,               /* parent handle            */
  385.                            lpProcAbout         /* About() instance address */
  386.                          );         
  387.                 FreeProcInstance( lpProcAbout );
  388.                 break;
  389.  
  390.               case IDM_ABOUT:
  391.                 lpProcAbout = MakeProcInstance( About, hInst );
  392.                 DialogBox(hInst,                  /* current instance         */
  393.                           "AboutBox",             /* resource to use         */
  394.                           hWnd,                   /* parent handle         */
  395.                           lpProcAbout);           /* About() instance address */
  396.                 FreeProcInstance( lpProcAbout );
  397.                 break;
  398.  
  399.               default:
  400.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  401.             }
  402.           break;
  403.  
  404.         case WM_DESTROY:          /* message: window being destroyed */
  405.           PostQuitMessage(0);
  406.           break;
  407.  
  408.         default:              /* Passes it on if unproccessed    */
  409.           return (DefWindowProc(hWnd, message, wParam, lParam));
  410.       }
  411.     return (NULL);
  412.   }
  413.  
  414.  
  415. /****************************************************************************
  416.   FUNCTION: About(HWND, unsigned, WORD, LONG)
  417.   PURPOSE:  Processes messages for "About" dialog box
  418.   MESSAGES:
  419.     WM_INITDIALOG - initialize dialog box
  420.     WM_COMMAND    - Input received
  421.   COMMENTS:
  422.     No initialization is needed for this particular dialog box, but TRUE
  423.     must be returned to Windows.
  424.     Wait for user to click on "Ok" button, then close the dialog box.
  425. ****************************************************************************/
  426. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  427. HWND hDlg;                                /* window handle of the dialog box */
  428. unsigned message;                         /* type of message                 */
  429. WORD wParam;                              /* message-specific information    */
  430. LONG lParam;
  431.   {
  432.     switch (message) {
  433.     case WM_INITDIALOG:           /* message: initialize dialog box */
  434.         return (TRUE);
  435.  
  436.     case WM_COMMAND:              /* message: received a command */
  437.         if (wParam == IDOK                /* "OK" box selected?         */
  438.                 || wParam == IDCANCEL) {      /* System menu close command? */
  439.         EndDialog(hDlg, TRUE);          /* Exits the dialog box         */
  440.         return (TRUE);
  441.         }
  442.         break;
  443.     }
  444.     return (FALSE);                  /* Didn't process a message    */
  445.   }
  446.